package com.sun.controller;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.sun.entity.User;
import com.sun.service.UserService;
import com.sun.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @author shkstart
 * @create 2021-09-21 14:34
 */
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/login")
    public Map<String,Object> login(User user){
        Map result = new HashMap<String, Object>();
        log.info("用户名:{}",user.getName());
        log.info("密码:{}",user.getPassword());
        try {
            User login = userService.login(user);
            Map<String, String> map = new HashMap<>();//用来存放payload
            map.put("id",user.getId());
            map.put("username",user.getName());
            String token = JwtUtils.getToken(map);
            result.put("state",true);
            result.put("msg","登录成功！！！");
            result.put("token",token);
        }catch (Exception e){
            e.printStackTrace();
            result.put("state","false");
            result.put("msg",e.getMessage());
        }
        return result;
    }

    /**
     * 测试token
     * @param token
     * @return
     */
    @PostMapping("/test/test")
    public String test(String token){

        return "sdf";
    }
    @GetMapping("/user/test")
    public String test(){
        return "nihao";
    }
}
